package cn.edu.cug.cs.gtl.gsm.item;

import cn.edu.cug.cs.gtl.common.Identifier;

import java.io.DataInput;
import java.io.DataOutput;
import java.util.*;

import cn.edu.cug.cs.gtl.common.Identifier;
import cn.edu.cug.cs.gtl.io.Storable;

import java.io.IOException;

public class IndexMap extends HashMap<Identifier,IndexItem> implements Storable {
    private static final long serialVersionUID = -8564179991254008357L;


    public IndexMap(){
        super();
    }


    @Override
    public boolean load(DataInput dataInput) throws IOException {
        int c = dataInput.readInt();
        if(c<=0) return false;
        this.clear();
        for(int i=0;i<c;++i){
            IndexItem indexItem = new IndexItem(0,new ArrayList<>());
            indexItem.load(dataInput);
            this.put(indexItem.getPageIdentifiers().get(0),indexItem );
        }
        return true;
    }

    @Override
    public boolean store(DataOutput dataOutput) throws IOException {
        Collection<IndexItem> its = this.values();
        int c = its.size();
        dataOutput.writeInt(c);
        for(IndexItem i: its){
            i.store(dataOutput);
        }
        return true;
    }
}
